import sys

from PyQt5.QtChart import QChartView, QChart, QSplineSeries, QValueAxis
from PyQt5.QtCore import Qt, QTimer, QPointF, QMargins
from PyQt5.QtGui import QPainter, QPen
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QGraphicsDropShadowEffect
#from model import DisPlayPlotConfig, EnvModel

class LineChart(QWidget):
    def __init__(self):
        super().__init__()
        self.historyModel = None
        self.currentModel = None
        self.config = None  # 保存数据
        # 初始化图像
        temperatureChart, self.temperatureSeries = self.createLineChart((0, 24), (0, 40), "时间", "温度", 13, 6, Qt.red)
        humidityChart, self.humiditySeries = self.createLineChart((0, 24), (0, 100), "时间", "湿度", 13, 6)
        lightChart, self.lightSeries = self.createLineChart((0, 24), (0, 130), "时间", "PM2_5", 13, 6, Qt.green)
        flowChart, self.flowSeries = self.createLineChart((0, 24), (0, 100), "时间", "运动量", 13, 6)
        temperatureChart.setMargins(QMargins(0, 20, 10, 0))
        humidityChart.setMargins(QMargins(0, 20, 10, 0))
        lightChart.setMargins(QMargins(0, 20, 10, 0))
        flowChart.setMargins(QMargins(0, 20, 10, 0))
        # 隐藏图例
        temperatureChart.legend().hide()
        humidityChart.legend().hide()
        lightChart.legend().hide()
        flowChart.legend().hide()
        # 去掉外边界和圆角
        temperatureChart.layout().setContentsMargins(0, 0, 0, 0)
        temperatureChart.setBackgroundRoundness(0)
        humidityChart.layout().setContentsMargins(0, 0, 0, 0)
        humidityChart.setBackgroundRoundness(0)
        lightChart.layout().setContentsMargins(0, 0, 0, 0)
        lightChart.setBackgroundRoundness(0)
        flowChart.layout().setContentsMargins(0, 0, 0, 0)
        flowChart.setBackgroundRoundness(0)

        self.temperatureChartView = QChartView(temperatureChart)
        self.humidityChartView = QChartView(humidityChart)
        self.lightChartView = QChartView(lightChart)
        self.flowChartView = QChartView(flowChart)
        self.temperatureChartView.setRenderHint(QPainter.Antialiasing)
        self.humidityChartView.setRenderHint(QPainter.Antialiasing)
        self.lightChartView.setRenderHint(QPainter.Antialiasing)
        self.flowChartView.setRenderHint(QPainter.Antialiasing)
        self.temperatureChartView.setMinimumSize(380, 150)
        self.humidityChartView.setMinimumSize(380, 150)
        self.lightChartView.setMinimumSize(380, 150)
        self.flowChartView.setMinimumSize(380, 150)
        self.flowChartView.hide()
        #self.temperatureChartView.setStyleSheet("margin: 0px, 0px, 0px, 0px;border:none;padding:0px, 0px, 0px, 0px")
        #self.humidityChartView.setMargins(QMargins(0, 20, 10, 0))
        #self.lightChartView.setMargins(QMargins(0, 20, 10, 0))

        # 布局
        layout = QVBoxLayout()
        layout.addWidget(self.temperatureChartView, Qt.AlignCenter)
        layout.addWidget(self.humidityChartView, Qt.AlignCenter)
        layout.addWidget(self.lightChartView, Qt.AlignCenter)
        layout.addWidget(self.flowChartView, Qt.AlignCenter)
        layout.setContentsMargins(0, 0, 0, 0)
        layout.setSpacing(10)
        self.setLayout(layout)

        # 背景透明
        self.setAttribute(Qt.WA_TranslucentBackground)
        self.setWindowFlags(self.windowFlags() | Qt.FramelessWindowHint)

    # 创建图表
    def createLineChart(self, xrange, yrange, xtitle, ytitle,
                        xtickCount, ytickCount, linecoolor=Qt.blue,
                        linewidth=1, theme=QChart.ChartThemeBrownSand):
        chart = QChart()
        series = QSplineSeries(chart)
        green_pen = QPen(linecoolor)
        #green_pen.setWidth(linewidth)

        axisX = QValueAxis()
        axisY = QValueAxis()
        axisX.setTitleText(xtitle)
        axisY.setTitleText(ytitle)
        chart.addSeries(series)
        chart.addAxis(axisX, Qt.AlignBottom)
        chart.addAxis(axisY, Qt.AlignLeft)
        series.attachAxis(axisX)
        series.attachAxis(axisY)
        axisX.setTickCount(xtickCount)
        axisY.setTickCount(ytickCount)
        axisX.setRange(xrange[0], xrange[1])
        axisY.setRange(yrange[0], yrange[1])
        #chart.setTheme(theme)
        series.setPen(green_pen)
        chart.setAnimationOptions(QChart.AllAnimations) # 开启动画

        return chart, series

    def display(self):
        self.show()

    def update_figure(self, data):
        self.config = data
        if self.temperatureSeries.count() > 0:
            self.temperatureSeries.clear()
            self.humiditySeries.clear()
            self.lightSeries.clear()
            self.flowSeries.clear()
        length = self.config.timex.__len__()
        for count in range(length):
            if length > 0:
                self.temperatureSeries.append(self.config.timex[count], self.config.tempy[count])
                self.humiditySeries.append(self.config.timex[count], self.config.humidity[count])
                self.lightSeries.append(self.config.timex[count], self.config.PM2D5[count])
                self.flowSeries.append(self.config.timex[count], self.config.lighty[count])

if __name__ == "__main__":
    app = QApplication(sys.argv)
    model = EnvModel()
    chart = LineChart()
    chart.setModel(model)
    chart.resize(500, 500)
    chart.show()
    sys.exit(app.exec_())
